package com.hgyc.mom.eam.controller;

import java.util.*;

import com.hgyc.mom.common.exception.BizException;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.eam.entity.EamCheckMaintainPlan;
import com.hgyc.mom.eam.vo.EamCheckMaintainPlanParam;
import com.hgyc.mom.eam.service.EamCheckMaintainPlanService;
import com.hgyc.mom.eam.vo.EamCheckMaintainPlanVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 设备点检保养计划Controller
 *
 * @author fwj
 * @since 2025-08-13
 */
@Tag(name = "设备点检保养计划控制器", description = "设备点检保养计划控制器")
@RestController
@RequestMapping("/eam/check/maintain/plan")
public class EamCheckMaintainPlanController {
    @Autowired
    private EamCheckMaintainPlanService eamCheckMaintainPlanService;

    @Operation(summary = "查询设备点检保养计划列表", description = "查询设备点检保养计划列表")
    @PostMapping("/pageList")
    public Result<IPage<EamCheckMaintainPlan>> pageList(@Parameter(description = "查询参数") @RequestBody EamCheckMaintainPlanParam eamCheckMaintainPlanParam) {
        Page<EamCheckMaintainPlan> page = new Page<>(eamCheckMaintainPlanParam.getPage(), eamCheckMaintainPlanParam.getPageSize());
        EamCheckMaintainPlan eamCheckMaintainPlan = new EamCheckMaintainPlan();
        BeanUtils.copyProperties(eamCheckMaintainPlanParam, eamCheckMaintainPlan);
        QueryWrapper<EamCheckMaintainPlan> wrapper = QueryWrapperUtil.build(eamCheckMaintainPlan);
        IPage<EamCheckMaintainPlan> list = eamCheckMaintainPlanService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取设备点检保养计划详细信息", description = "获取设备点检保养计划详细信息")
    @GetMapping(value = "/{id}")
    public Result<EamCheckMaintainPlan> getInfo(@Parameter(description = "设备点检保养计划ID") @PathVariable("id") Long id) {
        return Result.success(eamCheckMaintainPlanService.getById(id));
    }
    @Operation(summary = "新增设备点检保养计划", description = "新增设备点检保养计划")
    @PostMapping
    public Result<?> save(@Parameter(description = "设备点检保养计划") @RequestBody EamCheckMaintainPlanVO eamCheckMaintainPlanVO) {
        eamCheckMaintainPlanService.savePlan(eamCheckMaintainPlanVO);
        return Result.success();
    }

    @Operation(summary = "修改设备点检保养计划", description = "修改设备点检保养计划")
    @PutMapping
    public Result<?> update(@Parameter(description = "设备点检保养计划") @RequestBody EamCheckMaintainPlanVO eamCheckMaintainPlanVO) {
        eamCheckMaintainPlanService.updatePlan(eamCheckMaintainPlanVO);
        return Result.success();
    }

    @Operation(summary = "删除设备点检保养计划", description = "删除设备点检保养计划")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        eamCheckMaintainPlanService.removePlan(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "更新状态", description = "更新状态")
    @PostMapping("/status/change")
    public Result changeStatus(@RequestBody EamCheckMaintainPlan plan)
    {
        eamCheckMaintainPlanService.changeStatus(plan);
        return Result.success();
    }

    @Operation(summary = "创建点检保养任务", description = "创建点检保养任务")
    @PostMapping("/excute")
    public Result<?> excuteCreatePlan(@RequestBody EamCheckMaintainPlan plan) {
        if (plan.getId() == null) {
            throw new BizException("获取主键异常！");
        }
        eamCheckMaintainPlanService.excuteCreatePlan(plan.getId());
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EamCheckMaintainPlan> util = new ExcelUtil<>(EamCheckMaintainPlan.class);
        util.importTemplateExcel(response, "设备点检保养计划");
    }

    @Operation(summary = "导出设备点检保养计划列表", description = "导出设备点检保养计划列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamCheckMaintainPlanParam eamCheckMaintainPlanParam) {
        EamCheckMaintainPlan eamCheckMaintainPlan = new EamCheckMaintainPlan();
        BeanUtils.copyProperties(eamCheckMaintainPlanParam, eamCheckMaintainPlan);
        QueryWrapper<EamCheckMaintainPlan> wrapper = QueryWrapperUtil.build(eamCheckMaintainPlan);
        List<EamCheckMaintainPlan> list = eamCheckMaintainPlanService.list(wrapper);
        ExcelUtil<EamCheckMaintainPlan> util = new ExcelUtil<>(EamCheckMaintainPlan.class);
        util.exportExcel(response, list, "设备点检保养计划数据");
    }

    @Operation(summary = "导入设备点检保养计划列表", description = "导入设备点检保养计划列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EamCheckMaintainPlan> util = new ExcelUtil<>(EamCheckMaintainPlan.class);
        List<EamCheckMaintainPlan> eamCheckMaintainPlanList = util.importExcel(file.getInputStream());
        String message = eamCheckMaintainPlanService.importData(eamCheckMaintainPlanList, updateSupport);
        return Result.success(message);
    }
}
